package com.fenghongzhang.day011;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class MyListAdapter extends BaseAdapter {
    private Context context;
    private List<Person> list;

    public MyListAdapter(Context context, List<Person> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoler viewHoler;
        if(convertView == null){
            viewHoler = new ViewHoler();
            convertView = LayoutInflater.from(context).inflate(R.layout.person_lv_layout, null);
            viewHoler.person_name = convertView.findViewById(R.id.person_name);
            viewHoler.person_number = convertView.findViewById(R.id.person_number);
            convertView.setTag(viewHoler);
        }else {
            viewHoler = (ViewHoler) convertView.getTag();
        }

        Person person = list.get(position);
        viewHoler.person_number.setText(person.getNumber());
        viewHoler.person_name.setText(person.getName());
        return convertView;
    }

    class ViewHoler{
        TextView person_name;
        TextView person_number;
    }

}
